package com.webdesign.action;

import java.io.File;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.struts2.ServletActionContext;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionContext;
import com.webdesign.dto.MyPage;
import com.webdesign.entity.TbTeacher;
import com.webdesign.service.ITbTeacherService;

import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;


@Controller("TeachersStateAction")
@Scope("prototype")
public class TeachersStateAction {

	private ITbTeacherService iTbTeacherService;
	
	private List<TbTeacher> teachers;
	
	private String numpage;
	
	private MyPage page;
	
	private String usersid;
	
	public Boolean flag;
	
	private Map root = new HashMap();
	
	private String propath = ServletActionContext.getRequest().getRealPath("");
	
	private String path = ServletActionContext.getRequest().getContextPath();
	
	private Map<String,Object> session = ActionContext.getContext().getSession();
	
	private String username = (String) session.get("username");
	
	private Configuration cfg = new Configuration();
	
	/*
	 * 修改教师密码页面显示
	 */
	public String resetTeachersPwdShow() throws IOException, TemplateException{
		
		teachers = iTbTeacherService.resetTeachersPwdShow();
		for(TbTeacher teacher : teachers){
			teacher.setEmail(null);
			teacher.setPassword(null);
			teacher.setState(null);
		}
		
		if(page == null){
			page = new MyPage();
			page.setNumber(1);
		}
		
		Integer count = teachers.size();
		page.setTotalElements(count);
		if(numpage != null){
			page.setNumber(Integer.parseInt(numpage));
		}
		Integer sItem = (int)(page.getNumber() - 1) * page.getSize();
		Integer eItem = sItem + page.getSize();
		if(eItem > count){
			teachers = teachers.subList(sItem, count);
		}else{
			teachers = teachers.subList(sItem, eItem);
		}
		
		cfg.setDirectoryForTemplateLoading(new File(propath + "/template"));
		root.put("path", path);
		root.put("username", username);
		root.put("page", page);
		root.put("teachers", teachers);
		Template template = cfg.getTemplate("/admin/adminhomepage.ftl");
		Writer out = new OutputStreamWriter(System.out); 
		template.process(root, out);
		out.flush();
		
		return "success";
	}
	
	/*
	 * 重置教师密码
	 */
	public String resetTeacherPwd()
	{
		String[] teacherid = usersid.split(",");
		flag = iTbTeacherService.resetTeacherPwd(teacherid);
		return "success";
	}
	
	/*
	 * 首页显示未审核的教师信息
	 */
	public String showTeachers() throws IOException, TemplateException{
		
		teachers = iTbTeacherService.showteacher();
		for(TbTeacher teacher : teachers){
			teacher.setEmail(null);
			teacher.setPassword(null);
			teacher.setState(null);
		}
		
		if(page == null){
			page = new MyPage();
			page.setNumber(1);
		}
		
		Integer count = teachers.size();
		page.setTotalElements(count);
		if(numpage != null){
			page.setNumber(Integer.parseInt(numpage));
		}
		Integer sItem = (int)(page.getNumber() - 1) * page.getSize();
		Integer eItem = sItem + page.getSize();
		if(eItem > count){
			teachers = teachers.subList(sItem, count);
		}else{
			teachers = teachers.subList(sItem, eItem);
		}
		
		cfg.setDirectoryForTemplateLoading(new File(propath + "/template"));
		root.put("path", path);
		root.put("username", username);
		root.put("page", page);
		root.put("teachers", teachers);
		Template template = cfg.getTemplate("/admin/resetteacherspwd.ftl");
		Writer out = new OutputStreamWriter(System.out); 
		template.process(root, out);
		out.flush();
		
		return "success";
	}


	public ITbTeacherService getiTbTeacherService() {
		return iTbTeacherService;
	}


	@Resource(name="TbTeacherService")
	public void setiTbTeacherService(ITbTeacherService iTbTeacherService) {
		this.iTbTeacherService = iTbTeacherService;
	}

	public String getNumpage() {
		return numpage;
	}

	public void setNumpage(String numpage) {
		this.numpage = numpage;
	}

	public String getUsersid()
	{
		return usersid;
	}
	
	public void setUsersid(String usersid)
	{
		this.usersid=usersid;
	}
	
	public Boolean getFlag()
	{
		return flag;
	}
	
	public void setFlag(Boolean flag)
	{
		this.flag=flag;
	}

	public Map getRoot() {
		return root;
	}

	public void setRoot(Map root) {
		this.root = root;
	}
	
	
}
